/*
 * 5.0 crash
 * 
 * */

package com.onstar.cn.weight;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.onstar.cn.weight.anim.DotAnimTextView;

public class ProgressLayout extends LinearLayout {
    private CustomImageView imgAnim;
    private View progress_layout;
    private DotAnimTextView dot_view;

    private RoundProgress roundProgress;

    public ProgressLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initview(attrs);
    }

    private View view;

    public ProgressLayout(Context context) {
        super(context);
        initview(null);
    }

    public ProgressLayout(Context context, AttributeSet arrs) {
        super(context, arrs);
        initview(arrs);
    }

    private void initview(AttributeSet arrs) {
        view = inflate(getContext(), R.layout.weight_showprogress, this);
        if (progress_layout == null) {
            progress_layout = view.findViewById(R.id.progress_layout);
        }
        if (dot_view == null) {
            dot_view = (DotAnimTextView) view.findViewById(R.id.dot_view);
        }
//		if (imgAnim == null) {
//			imgAnim = (CustomImageView) view.findViewById(R.id.img_anim);
//		}
        if (roundProgress == null) {
            roundProgress = (RoundProgress) view.findViewById(R.id.round_progress);
        }
    }

    @SuppressWarnings("deprecation")
    private void loadBackground() {
        if (progress_layout.getBackground() == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                progress_layout.setBackground(getResources().getDrawable(R.drawable.weight_join_me_bg));
            } else {
                progress_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.weight_join_me_bg));
            }
        }
//        AnimationDrawable animationDrawable = (AnimationDrawable) imgAnim.getBackground();
//        if (animationDrawable == null) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//                imgAnim.setBackground(getResources().getDrawable(R.anim.weight_loading_anim));
//            } else {
//                imgAnim.setBackgroundDrawable(getResources().getDrawable(R.anim.weight_loading_anim));
//            }
//        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            roundProgress.setVisibility(View.GONE);
            dot_view.stop();
        } else if (visibility == View.VISIBLE) {
            loadBackground();
            roundProgress.setVisibility(View.VISIBLE);
            if (dot_view.isPlaying()) {
                dot_view.stop();
            }
            dot_view.show();
            dot_view.start();
        }
    }
}
